package com.dcivision.framework.taglib.html;


import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.bean.BeansSorter;
/**
 * @version $Revision: 1.5.2.2 $ $Date: 2007/07/23 07:43:38 $
 * @author  Ryan Kwan
 */
/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/07/04
 * BugID      : 5309
 * Description: Do not sort when name is 'userMemberList';
 * *********************************************************************************************
 */
public class BeanSorterTag extends BodyTagSupport{
    public static final String SORT_MODE_PARAM="sortOrder";
    public static final String SORT_ATTRIBUTE_PARAM="sortAttribute";
    
    private static final Log log = LogFactory.getLog(BeanSorterTag.class);
    
    /** Holds value of property id. */
    private String id;

    /** Holds value of property sortOrder. */
    private String sortOrder;

    /** Holds value of property sortAttribute. */
    private String sortAttribute="";

    /** Holds value of property property. */
    private String property;

    /** Holds value of property collection. */
    private List list;

    private String name;

    private String prefix="";

    private int scope=PageContext.SESSION_SCOPE;

    
    public int doStartTag() throws JspException {
        try{
            List sortedList = null;
            List unsortedList = (List)pageContext.getAttribute(name,scope);
            //log.debug("&&&&&&&&&&&&&&&&&unsortedList="+unsortedList);
            ServletRequest request = pageContext.getRequest();

            //String sortOrderParam = getAttribute(prefix+SORT_MODE_PARAM);
            //log.debug("IIIIIIIIIIIsortOrderPapam1="+sortOrderParam);
            //String sortAttributeParam = getAttribute(prefix+SORT_ATTRIBUTE_PARAM);

            /*if(sortOrderParam==null){
                log.debug("sortOrder");
                sortOrderParam=sortOrder;
            }*/
            //pageContext.getSession().setAttribute(prefix+SORT_MODE_PARAM,sortOrderParam);

            /*if(sortAttributeParam==null){
                log.debug("sortAttribute");
                sortAttributeParam=sortAttribute;
            }*/
            //pageContext.getSession().setAttribute(prefix+SORT_ATTRIBUTE_PARAM,sortAttributeParam);

            //log.debug(prefix+SORT_MODE_PARAM+":"+sortOrderParam);
            //log.debug(prefix+SORT_ATTRIBUTE_PARAM+":"+sortAttributeParam);

            setSortOrder(sortOrder);
            /* Jicle.Wang 5309 2006/07/04 Start */
            //if (sortAttribute== null || "".equals(sortAttribute) || "userMemberList".equals(name)) {
            if (sortAttribute== null || "".equals(sortAttribute) || "userMemberList".equals(name)) {
            /* Jicle.Wang 5309 2006/07/04 End   */
                log.debug("No Sorting Attribute Defined!");
                sortedList = unsortedList;
            } else {
                sortedList = BeansSorter.sort(unsortedList,sortAttribute,GlobalConstant.LIST_ACSENDING.equalsIgnoreCase(sortOrder) ? 0 : 1);
            }
            //log.debug("HHHHHHHHHHHHHHHHHHsortedList="+sortedList);
            // Special handle for DMS which make folders appeared at the beginning.
            /*if (sortedList != null && sortedList.size() > 0) {
                if (sortedList.get(0) instanceof com.dcivision.dms.bean.DmsDocument) {
                    log.debug("JJJJJJJ");
                    sortedList = BeansSorter.sort(sortedList, "documentType", 1);
                    log.debug("PPPPPPPPPPPPPsortedList1="+sortedList);
                }
            }*/
            pageContext.setAttribute(id,sortedList,scope);

            return this.EVAL_BODY_INCLUDE;
        }catch(Exception e){
          log.error(e, e);
          throw new JspException(e.getMessage());
        }

    }
    protected String getAttribute(String key){
        String value = (String)pageContext.getAttribute(key);
        if(value==null || value.length()==0){
            value=pageContext.getRequest().getParameter(key);
        }
        if(value==null || value.length()==0){
            value=(String)pageContext.getSession().getAttribute(key);
        }
        return value;
    }

    public void setScope(String scope){
        if("request".equals(scope)){
            this.scope = PageContext.REQUEST_SCOPE;
        }else if("session".equals(scope)){
            this.scope = PageContext.SESSION_SCOPE;
        }else if("page".equals(scope)){
            this.scope = PageContext.PAGE_SCOPE;
        }else if("application".equals(scope)){
            this.scope = PageContext.APPLICATION_SCOPE;
        }
    }

    
    public int doEndTag() throws JspException{
        return this.EVAL_PAGE;
    }


    /** Setter for property id.
     * @param id New value of property id.
     */
    
    public void setId(String id) {
        this.id = id;
    }

    /** Setter for property sortOrder.
     * @param sortOrder New value of property sortOrder.
     */
    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;//= "ascending".equalsIgnoreCase(sortOrder) ? 0 : 1;
    }

    public void setName(String name){
        this.name = name;
    }

    /** Setter for property sortAttribute.
     * @param sortAttribute New value of property sortAttribute.
     */
    public void setSortAttribute(String sortAttribute) {
        this.sortAttribute = sortAttribute;
    }

    /** Setter for property property.
     * @param property New value of property property.
     */
    public void setProperty(String property) {
        this.property = property;
    }


    /** Setter for property collection.
     * @param collection New value of property collection.
     */
    public void setList(List list) {
        this.list = list;
    }

    public void setPrefix(String prefix){
        this.prefix = prefix;
    }

}
